Bootstrap

django 实战(1): ubuntu安装django

因为CentOS不支持服务了,所以ubuntu成了很多使用Linux的首选。

本文以ubuntu20.4系统为基础,准备django环境。

1.  安装Anaconda

Anaconda则是一个打包的集合,里面预装好了conda、某个版本的python、众多packages、科学计算工具等等,所以也称为Python的一种发行版。

1.1 下载Anaconda

进入Anaconda官网下载:Free Download | Anaconda

也可以到镜像网站

 Index of /anaconda/archive/ | 清华大学开源软件镜像站 | Tsinghua Open Source Mirror 

下载Anaconda3-2023.03-1-Linux-x86_64.sh

至于是在windows系统下下载还是在ubuntu系统下载都无妨,在windows系统下载之后需要使用sftp上传至虚拟机的ubuntu中;如果在ubuntu中,也可使用火狐浏览器下载,上传则可省略。

1.2 安装Anaconda

xxx@xxx-virtual-machine:~/Downloads$ bash Anaconda3-2023.03-1-Linux-x86_64.sh

(注意:千万不要使用root用户安装,一定要是用普通用户进行安装,使用root用户安装的anaconda之后普通用户无法使用,会有一大堆的问题等着你处理。)

按yes同意协议后

按回车默认安装目录

1.3 环境变量初始化

安装完成后,提示是否要初始化。建议选择yes, 如下:

installation finished.

Do you wish the installer to initialize Anaconda3

by running conda init? [yes|no]

[no] >>> yes

no change     /home/yundo/anaconda3/condabin/conda

no change     /home/yundo/anaconda3/bin/conda

no change     /home/yundo/anaconda3/bin/conda-env

no change     /home/yundo/anaconda3/bin/activate

no change     /home/yundo/anaconda3/bin/deactivate

no change     /home/yundo/anaconda3/etc/profile.d/conda.sh

no change     /home/yundo/anaconda3/etc/fish/conf.d/conda.fish

no change     /home/yundo/anaconda3/shell/condabin/Conda.psm1

no change     /home/yundo/anaconda3/shell/condabin/conda-hook.ps1

no change     /home/yundo/anaconda3/lib/python3.10/site-packages/xontrib/conda.xsh

no change     /home/yundo/anaconda3/etc/profile.d/conda.csh

modified      /home/yundo/.bashrc

==> For changes to take effect, close and re-open your current shell. <==

If you'd prefer that conda's base environment not be activated on startup,

   set the auto_activate_base parameter to false:

conda config --set auto_activate_base false

Thank you for installing Anaconda3!

初始化后,anaconda将配置写入了~/.bashrc 文件。写入的内容如下:

# >>> conda initialize >>>

# !! Contents within this block are managed by 'conda init' !!

__conda_setup="$('/home/yundo/anaconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"

if [ $? -eq 0 ]; then

    eval "$__conda_setup"

else

    if [ -f "/home/yundo/anaconda3/etc/profile.d/conda.sh" ]; then

        . "/home/yundo/anaconda3/etc/profile.d/conda.sh"

    else

        export PATH="/home/yundo/anaconda3/bin:$PATH"

    fi

fiunset __conda_setup

# <<< conda initialize <<<

1.4 使配置文件生效

xxx@xxx-virtual-machine:~$  source ~/.bashrc

1.5 验证是否安装成功

使用conda –version或conda -V查看anaconda版本,若显示版本号,则安装成功。

(base) xxx@xxx-virtual-machine:~$ conda -V

conda 23.3.1

(base) xxx@xxx-virtual-machine:~$ python

Python 3.10.9 (main, Mar  1 2023, 18:23:06) [GCC 11.2.0] on linux

Type "help", "copyright", "credits" or "license" for more information.

>>>

1.6 配置镜像源

使用conda进行安装时,访问的是国外的网络,所以下载和安装包时会特别慢。我们需要更换到国内镜像源地址,这里更换到国内的清华大学地址。(永久添加镜像)

1.6.1 查看anaconda中已经存在的镜像源

(base) xxx@xxx-virtual-machine:~$ conda config --show channels

channels:

  - defaults

1.6.2 添加镜像源(永久添加)

(base)xxx@xxx-virtual-machine:~$ conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/

(base) xxx@xxx-virtual-machine:~$ conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/

(base) xxx@xxx-virtual-machine:~$ conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/

(base)xxx@xxx-virtual-machine:~$

 1.6.3 设置搜索时显示通道地址

(base)xxx@yundoxxx-virtual-machine:~$ conda config --set show_channel_urls yes

1.6.4 添加完后查看

(base)xxx@xxx-virtual-machine:~$ conda config --show channels

channels:

  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/

  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/

  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/

  - defaults

 1.6.5 可以在命令中指定安装镜像地址

若不想按照上述步骤添加镜像,可使用以下命令直接指定安装时使用的镜像地址(以opencv为例):

(base) xxx@xxx-virtual-machine:~$ conda install opencv -c https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/ 

 2 安装django

用pip在命令行,直接加镜像源方式安装django

2.1 安装信息如下

(base) xxx@xxx-virtual-machine:~$ pip install Django -i https://mirrors.aliyun.com/pypi/simple/

Looking in indexes: https://mirrors.aliyun.com/pypi/simple/

Collecting Django

  Downloading https://mirrors.aliyun.com/pypi/packages/12/13/78e8622180f101e95297965045ff1325ea7301c1b80f756debbeaa84c3be/Django-4.2.1-py3-none-any.whl (8.0 MB)

     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 8.0/8.0 MB 5.7 MB/s eta 0:00:00

Collecting asgiref<4,>=3.6.0

  Downloading https://mirrors.aliyun.com/pypi/packages/8f/29/38d10a47b322a77b2d12c2b79c789f52956f733cb701d4d5157c76b5f238/asgiref-3.6.0-py3-none-any.whl (23 kB)

Collecting sqlparse>=0.3.1

  Downloading https://mirrors.aliyun.com/pypi/packages/98/5a/66d7c9305baa9f11857f247d4ba761402cea75db6058ff850ed7128957b7/sqlparse-0.4.4-py3-none-any.whl (41 kB)

     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 41.2/41.2 kB 4.5 MB/s eta 0:00:00

Installing collected packages: sqlparse, asgiref, Django

Successfully installed Django-4.2.1 asgiref-3.6.0 sqlparse-0.4.4

(base) yundo@yundo-virtual-machine:~$

2.1 验证是否安装成功

(base) xxx@xxx-virtual-machine:~$ python

Python 3.10.9 (main, Mar  1 2023, 18:23:06) [GCC 11.2.0] on linux

Type "help", "copyright", "credits" or "license" for more information.

>>> import django

>>> django.VERSION

(4, 2, 1, 'final', 0)

>>>

;