【Ubuntu】安装常用软件包

发布于:2024-09-18 ⋅ 阅读:(10) ⋅ 点赞:(0)

安装java

直接输入java,如果没有安装的话会提醒你输入命令安装,类似

Command 'java' not found, but can be installed with:
sudo apt install jdkxxxxxxxxxxxxxx

然后选一个版本安装就好,我这里选的jdk17,安装完确认一下

ubuntu@VM-4-13-ubuntu:~$ java --version
openjdk 17.0.12 2024-07-16
OpenJDK Runtime Environment (build 17.0.12+7-Ubuntu-1ubuntu224.04)
OpenJDK 64-Bit Server VM (build 17.0.12+7-Ubuntu-1ubuntu224.04, mixed mode, sharing)

安装python3

输入python看他的意思是有python3,输入python3后果然进入shell了,那就ok了。

ubuntu@VM-4-13-ubuntu:~$ python
Command 'python' not found, did you mean:
  command 'python3' from deb python3
  command 'python' from deb python-is-python3
ubuntu@VM-4-13-ubuntu:~$ python3
Python 3.12.3 (main, Apr 10 2024, 05:33:47) [GCC 13.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 

下面那句是你想要输入python时候就运行python3,可以安装 python-is-python3包,安装后输入python就也可以了,不过保持着能运行就不动原则,我没有操作这个,这步可选
sudo apt install python-is-python3

安装pip

发现pip和pip3也没有,自动提示了安装的命令,还挺方便的

ubuntu@VM-4-13-ubuntu:~$ pip
Command 'pip' not found, but can be installed with:
sudo apt install python3-pip
ubuntu@VM-4-13-ubuntu:~$ pip3
Command 'pip3' not found, but can be installed with:
sudo apt install python3-pip
ubuntu@VM-4-13-ubuntu:~$ 

运行提示的 sudo apt install python3-pip 就可以了,发现pip和pip3都存在,并不是指向一个命令,输入pip list/pip3 list 都没什么问题

ubuntu@VM-4-13-ubuntu:~$ which pip
/usr/bin/pip
ubuntu@VM-4-13-ubuntu:~$ which pip3
/usr/bin/pip3
ubuntu@VM-4-13-ubuntu:~$ ls -lsa /usr/bin/pip
4 -rwxr-xr-x 1 root root 221 Mar  6  2024 /usr/bin/pip
ubuntu@VM-4-13-ubuntu:~$ ls -lsa /usr/bin/pip3
4 -rwxr-xr-x 1 root root 221 Mar  6  2024 /usr/bin/pip3

然后问题就来了,安装时候报错
pip install xxxx

ubuntu@VM-4-13-ubuntu:~$ pip install xxxx
error: externally-managed-environment

× This environment is externally managed
╰─> To install Python packages system-wide, try apt install
    python3-xyz, where xyz is the package you are trying to
    install.
    
    If you wish to install a non-Debian-packaged Python package,
    create a virtual environment using python3 -m venv path/to/venv.
    Then use path/to/venv/bin/python and path/to/venv/bin/pip. Make
    sure you have python3-full installed.
    
    If you wish to install a non-Debian packaged Python application,
    it may be easiest to use pipx install xyz, which will manage a
    virtual environment for you. Make sure you have pipx installed.
    
    See /usr/share/doc/python3.12/README.venv for more information.

解释一下

这个错误信息是 Python 在 Ubuntu 系统上对于系统级包管理的保护机制。从 Python 3.7 开始,Python 包管理器 pip 被设计为默认不安装包到系统级路径,以避免潜在的系统冲突和安全问题。这个机制被称为 “externally-managed-environment”。

错误信息提供了几种解决方案:

  • 使用 apt 命令安装系统级 Python 包:
    sudo apt install python3-xyz

  • 创建虚拟环境:
    如果你想要安装的包不是 Debian 官方仓库中的,你可以创建一个虚拟环境。虚拟环境允许你在不影响系统其他部分的情况下安装和管理 Python 包。使用以下命令创建虚拟环境:

    python3 -m venv path/to/venv
    然后激活虚拟环境:

    source path/to/venv/bin/activate
    在虚拟环境中,你可以使用 pip 来安装包,这些包将只在这个虚拟环境中可用。

  • 使用 pipx 管理非 Debian 打包的 Python 应用:
    pipx 是一个工具,用于在隔离的环境中安装和运行 Python 应用程序。它会自动创建虚拟环境。使用以下命令安装 pipx:
    sudo apt install pipx
    pipx install xyz

  • 覆盖系统包:
    如果你确定要覆盖系统包,可以通过添加 --break-system-packages 选项来使用 pip 安装。但这样做可能会破坏你的 Python 安装或操作系统,因此不推荐这样做,除非你非常清楚你在做什么。

看下来还是最后一点最简单啊,哈哈,先按建议的来试试
pip list看了一眼没有pandas

ubuntu@VM-4-13-ubuntu:~$ python3
Python 3.12.3 (main, Jul 31 2024, 17:43:48) [GCC 13.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pandas
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'pandas'
>>> 
ubuntu@VM-4-13-ubuntu:~$ sudo apt install python3-pandas 
Reading package lists... Done
Building dependency tree... Done
.....
省略安装日志
.....
ubuntu@VM-4-13-ubuntu:~$ 
ubuntu@VM-4-13-ubuntu:~$ pip3 list |grep "pandas"
pandas                2.1.4+dfsg
ubuntu@VM-4-13-ubuntu:~$ 

ok搞定了,再试下最后一种方法,先卸载掉,重新安装一下
pip uninstall pandas
报上面的错,直接加上 –break-system-packages
pip uninstall pandas --break-system-packages
报错,权限不够,加sudo
sudo pip uninstall pandas --break-system-packages
卸载成功,emmm,好像不用重新安装了,–break-system-packages 也挺好用的,另外几种就不测试了,不喜欢维护每个环境

安装mysql

sudo apt install mysql-server-8.0
安装完之后就自己启动了

ubuntu@VM-4-13-ubuntu:~$ service mysql status
● mysql.service - MySQL Community Server
     Loaded: loaded (/usr/lib/systemd/system/mysql.service; enabled; preset: enabled)
     Active: active (running) since Tue 2024-09-10 13:45:19 CST; 1 day 1h ago
    Process: 2098809 ExecStartPre=/usr/share/mysql/mysql-systemd-start pre (code=exited, status=0/SUCCESS)
   Main PID: 2098818 (mysqld)
     Status: "Server is operational"
      Tasks: 38 (limit: 1969)
     Memory: 372.7M (peak: 379.7M)
        CPU: 3min 28.439s
     CGroup: /system.slice/mysql.service
             └─2098818 /usr/sbin/mysqld

然后sudo mysql登陆进去,这里不需要密码
在这里插入图片描述

ubuntu@VM-4-13-ubuntu:~$ sudo mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 14
Server version: 8.0.39-0ubuntu0.24.04.2 (Ubuntu)

Copyright (c) 2000, 2024, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> select user();
+----------------+
| user()         |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)

我猜测原因是,linux当前用户是 root,mysql的root没密码,所以直接登录成功(因为不用root登录时候提示ubuntu这个用户不对)

ubuntu@VM-4-13-ubuntu:~$ mysql
ERROR 1045 (28000): Access denied for user 'ubuntu'@'localhost' (using password: NO)

改了root密码后,用sudo 也提示无法访问了

ubuntu@VM-4-13-ubuntu:~$ sudo mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

然后再回来说下改root密码流程,就2步 thisUpassword替换为你自己的密码
1.ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'thisUpassword';
2.FLUSH PRIVILEGES;

mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'thisUpassword';
Query OK, 0 rows affected (0.02 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

然后exit退出,再使用客户端登录,这里就不用sudo了,直接指定用户名和密码就可以了

ubuntu@VM-4-13-ubuntu:~$ mysql -uroot -p
Enter password:

正常登录,搞定。如果你想新建其他用户的话,命令如下:
mysql> create user 'test'@'localhost' identified with mysql_native_password by '12345678';
mysql> grant all on *.* to 'test'@'localhost' with grant option;
mysql> flush privileges;

上面的localhost表示只能本地连接,如果你要远程连接的话改成 %
create user 'test'@'%' identified with mysql_native_password by '12345678';
如果已经创建好了,就更新一下属性
use mysql
update user set host='%' where user='test';

主要是这个表,改完记得刷新权限flush privileges;

mysql> select host,user from mysql.user;
+-----------+------------------+
| host      | user             |
+-----------+------------------+
| %         | test             |
| localhost | root             |
+-----------+------------------+

然后改一个配置 /etc/mysql/mysql.conf.d/mysqld.cnf 里面的这两行改成0.0.0.0,这个就不手把手教了

ubuntu@VM-4-13-ubuntu:/etc/mysql/mysql.conf.d$ cat /etc/mysql/mysql.conf.d/mysqld.cnf |grep "bind"
bind-address		= 127.0.0.1
mysqlx-bind-address	= 127.0.0.1

改完重启一下MySQL ,service mysql restart
然后就是防火墙了,云服务器一般会带,开放一下3306。
在这里插入图片描述

应该就可以了,然后附一个官方文档

后续再补充…


网站公告

今日签到

点亮在社区的每一天
去签到