Ubuntu装机

Ubuntu装机

Ubuntu 22.04 LTS 装系统记录,以留后用

安装系统

官方指引

Install Ubuntu desktop | Ubuntu

主要步骤

下载镜像

下载网址:

Enterprise Open Source and Linux | Ubuntu

制作USB启动盘

官方举例工具balenaEtcher ,工具网址:

balenaEtcher - Flash OS images to SD cards & USB drives

安装

安装期间可选中文,有:

  • 安装额外软件选项。(为更快安装,选普通安装,其他选项均不勾,可后期安装)

安装选项

  • 硬盘选项

  • 后略…

安装后

换软件源

1 简单方法(GUI)

在Settings里的最后一栏About,最后一栏Software Updates,里面有个Download from,从这里找到中国,然后直接自动测试连接最快的服务器就可以了

2 命令行方式

通用方式

lsb_release -a

release就是该ubuntu的版本号

codename就是该版本的代号

  1. 备份系统源
1
2
cd /etc/apt
sudo mv sources.list sources.list.bak
  1. 开始修改sources文件

有安装vim用户:

sudo vim sources.list

无安装vim用户:

sudo gedit sources.list

  1. a-(以阿里云为例)写入阿里云的源(版本:Ubuntu 22.04)
1
2
3
4
5
6
7
8
9
10
deb http://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy-proposed main restricted universe multiverse
  1. b-(以清华为例)写入清华的源(版本:Ubuntu 22.04)
1
2
3
4
5
6
7
8
9
10
11
12
13
# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security main restricted universe multiverse

# 预发布软件源,不建议启用
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-proposed main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-proposed main restricted universe multiverse
  1. c-各源的版本问题

以上各源中,jammy替换为其他版本代号,就是其他版本的源。

3 命令替换

执行如下命令自动替换(清华)

1
2
sudo sed -i "s@http://.*archive.ubuntu.com@https://mirrors.tuna.tsinghua.edu.cn@g" /etc/apt/sources.list
sudo sed -i "s@http://.*security.ubuntu.com@https://mirrors.tuna.tsinghua.edu.cn@g" /etc/apt/sources.list

4 可查看源官方公告

阿里镜像站

清华镜像站

安装更新

sudo apt update

sudo apt upgrade

固件升级

要显示 fwupd 检测到的所有设备:

1
fwupdmgr get-devices

注意: 列表中的部分设备可能不能使用该工具更新,例如 Intel 核芯显卡,可以替代地提供的供应商解决方案。

要从 Linux Vendor firmware Service (LVFS) 下载最新的元数据:

1
fwupdmgr refresh

要列出系统上任何设备可用的更新:

1
fwupdmgr get-updates

要安装更新:

1
fwupdmgr update

注意:

  • 可以实时应用的更新将立即完成。
  • 在启动时运行的更新将为下一次重新启动做好准备。
  • 可能需要使用 root 用户执行特定设备的更新。

python 换源

1
2
pip3 install -i https://mirrors.aliyun.com/pypi/simple/ -U pip
pip3 config set global.index-url https://mirrors.aliyun.com/pypi/simple/

其他镜像源:

清华:https://pypi.tuna.tsinghua.edu.cn/simple

阿里云:http://mirrors.aliyun.com/pypi/simple/

中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/

华中理工大学:http://pypi.hustunique.com/

山东理工大学:http://pypi.sdutlinux.org/

豆瓣:http://pypi.douban.com/simple/

设置合盖不休眠

解决方法:修改Login Manager配置文件。

打开终端,编辑文档:

1
sudo vim /etc/systemd/logind.conf

找到其中一行:

# HandleLidSwitch=suspend

将其改为:
HandleLidSwitch=ignore
注意前面的 # 也要去掉

然后重启服务:

1
sudo restart systemd-logind

或者

1
service systemd-logind restart

或者

1
sudo shutdown -r now

必要软件安装

安装软件方法

  1. *.deb包

sudo dpkg -i *.deb

即安装

  1. *.tar.gz(见下文,以FreeFileSync为例)

Visual Studio Code

可官网下载安装deb包;也可直接商店下载。

Git

安装
1
sudo apt install git

确认安装并检查已安装的 Git 构建版本:

1
git --version
配置
  1. 创建用户

分别输入用户名和邮箱

1
2
git config --global user.name "Albert_8700"
git config --global user.email albertlily@163.com
  1. 创建SSH Key

在用户主目录下,看看有没有.ssh目录,
如果有,

再看看这个目录下有没有id_rsa和id_rsa.pub这两个文件,

如果已经有了,可直接跳到下一步。
如果没有,打开Shell(Windows下打开Git Bash),创建SSH Key:

1
ssh-keygen -t rsa -C albertlily@163.com

可以在用户主目录里找到.ssh目录,里面有id_rsa和id_rsa.pub两个文件,这两个就是SSH Key的秘钥对,
id_rsa是私钥,不能泄露出去,
id_rsa.pub是公钥,可以放心地告诉任何人。

SSH

安装所需包

1
sudo apt install openssh-server

检查服务器状态(输入 q 退出该页面)

1
service ssh status

你还可以使用 systemctl 命令:

1
sudo systemctl status ssh

如果你的结果中 SSH 服务没有运行,使用这个命令运行它:

1
sudo systemctl enable --now ssh

允许 SSH 通过防火墙

1
sudo ufw allow ssh

Docker

详见docker官网

Install Docker Engine on Ubuntu | Docker Documentation

Uninstall old versions

Older versions of Docker went by the names of docker, docker.io, or docker-engine. Uninstall any such older versions before attempting to install a new version:

1
sudo apt-get remove docker docker-engine docker.io containerd runc

It’s OK if apt-get reports that none of these packages are installed.

Images, containers, volumes, and networks stored in /var/lib/docker/ aren’t automatically removed when you uninstall Docker. If you want to start with a clean installation, and prefer to clean up any existing data, refer to the uninstall Docker Engine section.

Installation methods

You can install Docker Engine in different ways, depending on your needs:

Docker Engine comes bundled with Docker Desktop for Linux. This is the easiest and quickest way to get started.

You can also set up and install Docker Engine from Docker’s apt repository.

Install it manually and manage upgrades manually.

Using a convenience scripts. Only recommended for testing and development environments.

  • Install using the repository

Before you install Docker Engine for the first time on a new host machine, you need to set up the Docker repository. Afterward, you can install and update Docker from the repository.

  1. Set up the repository

Update the apt package index and install packages to allow apt to use a repository over HTTPS:

1
2
3
4
5
6
sudo apt-get update
sudo apt-get install \
ca-certificates \
curl \
gnupg \
lsb-release
  1. Add Docker’s official GPG key:
1
2
sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
  1. Use the following command to set up the repository:
1
2
3
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
Install Docker Engine
  1. Update the apt package index:
1
sudo apt-get update

Receiving a GPG error when running apt-get update?

Your default umask may be incorrectly configured, preventing detection of the repository public key file. Try granting read permission for the Docker public key file before updating the package index:

1
2
sudo chmod a+r /etc/apt/keyrings/docker.gpg
sudo apt-get update
  1. Install Docker Engine, containerd, and Docker Compose.
  • Latest
  • Specific version

To install the latest version, run:

1
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-compose-plugin
  1. Verify that the Docker Engine installation is successful by running the hello-world image:
1
sudo docker run hello-world

This command downloads a test image and runs it in a container. When the container runs, it prints a confirmation message and exits.

You have now successfully installed and started Docker Engine. The docker user group exists but contains no users, which is why you’re required to use sudo to run Docker commands. Continue to Linux post-install to allow non-privileged users to run Docker commands and for other optional configuration steps.

Upgrade Docker Engine

To upgrade Docker Engine, follow the installation instructions, choosing the new version you want to install.

  • Install from a package

  • If you can’t use Docker’s apt repository to install Docker Engine, you can download the deb file for your release and install it manually. You need to download a new file each time you want to upgrade Docker Engine.

Go to https://download.docker.com/linux/ubuntu/dists/.

Select your Ubuntu version in the list.

Go to pool/stable/ and select the applicable architecture (amd64, armhf, arm64, or s390x).

Download the following deb files for the Docker Engine, CLI, containerd, and Docker Compose packages:

1
2
3
4
containerd.io_<version>_<arch>.deb
docker-ce_<version>_<arch>.deb
docker-ce-cli_<version>_<arch>.deb
docker-compose-plugin_<version>_<arch>.deb

Install the .deb packages. Update the paths in the following example to where you downloaded the Docker packages.

1
2
3
4
sudo dpkg -i ./containerd.io_<version>_<arch>.deb \
./docker-ce_<version>_<arch>.deb \
./docker-ce-cli_<version>_<arch>.deb \
./docker-compose-plugin_<version>_<arch>.deb

The Docker daemon starts automatically.

Verify that the Docker Engine installation is successful by running the hello-world image:

1
sudo docker run hello-world

This command downloads a test image and runs it in a container. When the container runs, it prints a confirmation message and exits.

You have now successfully installed and started Docker Engine. The docker user group exists but contains no users, which is why you’re required to use sudo to run Docker commands. Continue to Linux post-install to allow non-privileged users to run Docker commands and for other optional configuration steps.

向日葵

有桌面版和命令行版两个选择。

详见向日葵 for Linux

其中会出现依赖问题,通用方式解决。

命令行版安装步骤 (命令行版不能进行桌面控制,建议安装桌面版)

  1. 下载
    wget https://dl-cdn.oray.com/sunlogin/linux/sunloginclientshell-10.1.1.28779-amd64.deb

  2. 安装
    sudo dpkg -i 文件名.deb

  3. 启动
    /usr/local/sunlogin/bin/sunloginclient

卸载 方法:

1
sudo dpkg -r sunloginclient

Freefilesync

下载(也可以在浏览器下载):

1
wget https://freefilesync.org/download/FreeFileSync_11.0_Linux.tar.gz

解压:

1
tar xvf FreeFileSync_11.0_Linux.tar.gz

解压之后进入FreeFileSync文件夹,就可以双击运行程序了。

我们可以创建一个启动器,这样我们可以从桌面运行这个程序了,修改FreeFileSync.desktop文件中的Exec和Icon的路径,选择你解压FreeFileSync的位置:

1
2
3
4
5
6
7
8
9
10
11
12
13
cd /home/bob/FreeFileSync/
cat FreeFileSync.desktop
[Desktop Entry]
Type=Application
Name=FreeFileSync
GenericName=Folder Comparison and Synchronization
GenericName[de_DE]=Ordnervergleich und Synchronisation
Exec=/home/bob/FreeFileSync/FreeFileSync %F
Icon=/home/bob/FreeFileSync/Resources/FreeFileSync.png
NoDisplay=false
Terminal=false
Categories=Utility;FileTools;
StartupNotify=true

并将FreeFileSync.desktop文件拷贝到/usr/share/application目录中:

1
sudo cp -p /home/bob/FreeFileSync/FreeFileSync.desktop /usr/share/applications/

Done

多版本python共存问题

以下是18.04版本装机时的参考

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
> Ubuntu18.04兼容Python2.7、Python3.6、Python3.8以及pip、pip2、pip3问题
>
> 此为记录我重装Ubuntu后安装Python的过程
> 安装Python3.8
>  目前Ubuntu自带的Python版本为3.6,但官方都出3.8了,能不试一下新的功能吗?
> 第一步:下载Python3.8的源码
>
> 点击这里打开网址
> 选择下载tgz包(“Download Gzipped source tarball ”),解压
> 第二步:更新包列表并安装构建Python源所需的包
>
> 输入这三个命令
> sudo apt update
> sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev wget
> sudo apt-get install make
> 第三步:安装Python3.8
> cd 到Python解压目录,然后输入以下命令
>
> sudo ./configure --enable-optimizations
> sudo make altinstall
> 到这里Python3.8已经安装完成,但是只能输入python3.8打开,这里等到下面一并解决
>
> 安装pip和pip3直接输入,安装 
>
> sudo apt install python-pip
> sudo apt install python3-pip
> 此时,ubuntu给我安装了Python2.7、pip、pip2、pip3
> 解决兼容问题
>   我的想法是:Python2.7、Python3.6、Python3.8之间可以随便切换,而且pip和pip3会动态指向我切换的Python版本上(pip2就让他指向Python2.7吧)
> 第一步:解决Python版本切换问题
> 目前网上已经有很好的教程了,使用update-alternatives 来为整个系统更改Python 版本的方法更好。
> 找到各个Python的路径,使用which命令
>
> which python2.7
> which python3.6
> which python3.8
> 记录好他们所在路径,我这里是
>
> /usr/bin/python2.7
>
> /usr/bin/python3.6
>
> /usr/local/bin/python3.8
> 接下来就是重点了,使用update-alternatives命令:
> sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
> sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.6 2
> sudo update-alternatives --install /usr/bin/python python /usr/local/bin/python3.8 3
>   这里的最后一个参数是优先级,数越大越高,倒数第二个参数是各个版本Python所在位置,即我们上一步记录的东西
>   输入:
>     sudo update-alternatives --config python
>  选择并切换Python版本,至此,Python版本兼容性问题已经解决
>  
> 第二步:解决pip、pip2、pip3指向问题
>
>  当我切换到Python3.8输入pip -V,查看pip指向哪里之时,会报错“ from pip import main ImportError: cannot import name 'main' ”
> 听网上说,主要是由于ubuntu升级pip的版本后导致的,
> 我的做法是修改pip文件
> 注:一定要备份pip文件,包括pip、pip2、pip3、pip3.8(Python3.8的)
>
> 输入echo $PATH,可以看到环境变量:
> /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
>
> 不巧的是pip和pip3在/usr/local/bin目录下,而/usr/bin目录下也有pip和pip3,由优先级知道输入pip命令会优先执行/usr/local/bin目录下的。
> 所以我这里修改的是/usr/local/bin下的pip、pip3、pip3.8和/usr/bin下的pip2,假如想改/usr/bin下的pip和pip3的话可以直接cp过去(注意先备份)
> 下面是修改的代码,可以直接复制粘贴过去
> pip
>
> #!/usr/bin/python
>
> # GENERATED BY DEBIAN
>
> import sys
>
> # Run the main entry point, similarly to how setuptools does it, but because
>
> # we didn't install the actual entry point from setup.py, don't use the
>
> # pkg_resources API.
>
> if sys.version_info.major == 3 and sys.version_info.minor == 8:
> from pip import __main__
>
> if __name__ == '__main__':
> sys.exit(__main__._main())
> else:
> from pip import main
> if __name__ == '__main__':
> sys.exit(main())
> pip2
>
> #!/usr/bin/python2.7
>
> # GENERATED BY DEBIAN
>
> import sys
>
> # Run the main entry point, similarly to how setuptools does it, but because
>
> # we didn't install the actual entry point from setup.py, don't use the
>
> # pkg_resources API.
>
> from pip import main
> if __name__ == '__main__':
> sys.exit(main())
> pip3和pip3.8
>
> #!/usr/bin/python
>
> # -*- coding:utf-8 -*-
>
> # GENERATED BY DEBIAN
>
> import sys
> import os
>
> # Run the main entry point, similarly to how setuptools does it, but because
>
> # we didn't install the actual entry point from setup.py, don't use the
>
> # pkg_resources API.
>
> if sys.version_info.major == 3:
> if sys.version_info.minor == 8:
> from pip import __main__
>
> if __name__ == '__main__':
> sys.exit(__main__._main())
> else:
> from pip import main
>
> if __name__ == '__main__':
> sys.exit(main())
> else:
> msg = "当前Python版本为Python%s.%s,不能使用pip3!" % (sys.version_info.major, sys.version_info.minor)
> os.system("echo %s" % msg)
> os.system("""echo "请使用'update-alternatives --config python' 切换Python版本" """)
>
>     
> 参考资料:
>
> https://www.jb51.net/article/163112.htm
> https://blog.csdn.net/qq_38522539/article/details/80678412